/**
 * 
 */
package com.pcbly.web.persistence.chart;

import java.util.List;

import com.pcbly.web.bean.chart.ChartDataSourceBean;
import com.pcbly.web.persistence.HibernateBaseDao;

/**
 * @author Administrator
 * 
 */
public class ChartDataSourceHibernateDao extends
		HibernateBaseDao<ChartDataSourceBean> implements ChartDataSourceDao {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1577334123054309843L;

	public ChartDataSourceHibernateDao() {
		super();
	}

	protected Class<ChartDataSourceBean> getEntityClass() {
		return ChartDataSourceBean.class;
	}

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub

	}
	@Override
	public int deleteChartDataSourceBean(String id) throws Exception {
		// TODO Auto-generated method stub
		super.removeById(entityClass, id);
		return 1;
	}

	@Override
	public int insertChartDataSourceBean(ChartDataSourceBean bean)
			throws Exception {
		// TODO Auto-generated method stub
		getHibernateTemplate().save(bean);
		return 1;
	}

	@Override
	public ChartDataSourceBean queryChartDataSourceBeanById(String id)
			throws Exception {
		// TODO Auto-generated method stub
		return super.get(entityClass, id);
	}

	@Override
	public int updateChartDataSourceBean(ChartDataSourceBean bean)
			throws Exception {
		// TODO Auto-generated method stub
		getHibernateTemplate().update(bean);
		return 1;
	}
	@SuppressWarnings("unchecked")
	public List queryDataTableByBean(ChartDataSourceBean bean)throws  Exception{
		List list=getSession().createSQLQuery(buildString(bean).toString()).list();
		return list;
		
	}
	protected StringBuilder buildString(ChartDataSourceBean bean){
		StringBuilder sb=new StringBuilder();
		String m_Table=bean.getTableName();
		String m_Field=bean.getField();
		if (!(m_Table==null||"".equals(m_Table)||m_Field==null||"".equals(m_Field))){
			sb.append("select ");
			if(m_Field.indexOf("count")>=0&&m_Field.indexOf("(")<0){
				sb.append(m_Field);
				sb.append(" (*) ");
			}else{
				sb.append(m_Field);
			}
			sb.append("  ");
			sb.append(" from ");
			sb.append( m_Table );
			m_Table=bean.getCond();
			if(!(m_Table==null||"".equals(m_Table))){
				sb.append(" where ");
				sb.append( m_Table );
			}
			
			m_Table=bean.getOrder();
			if(!(m_Table==null||"".equals(m_Table))){
				sb.append(" order by  ");
				sb.append( m_Table );
			}
			
		}
		return sb;
	}
	@Override
	public List<ChartDataSourceBean> queryList() throws Exception {
		// TODO Auto-generated method stub
		return super.getAll(entityClass);
	}
}
